package cn.zust.itcost.controller;

import cn.zust.itcost.service.PdfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Api(tags = "PDF解析接口")
@RestController
@CrossOrigin(origins = "http://localhost:8090")
@RequestMapping("/api/pdf")
public class PdfController {

    @Autowired
    private PdfService pdfService;

    @ApiOperation("解析PDF文件")
    @PostMapping("/parse")
    public ResponseEntity<Map<String, Object>> parsePdf(@RequestParam("file") MultipartFile file) throws IOException {
        return ResponseEntity.ok(pdfService.parsePdf(file));
    }

    @ApiOperation("提取PDF文本内容")
    @PostMapping("/extract-text")
    public ResponseEntity<String> extractText(@RequestParam("file") MultipartFile file) throws IOException {
        return ResponseEntity.ok(pdfService.extractText(file));
    }

    @ApiOperation("获取PDF元数据")
    @PostMapping("/metadata")
    public ResponseEntity<Map<String, String>> getMetadata(@RequestParam("file") MultipartFile file) throws IOException {
        return ResponseEntity.ok(pdfService.getMetadata(file));
    }
}
